﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using NLog;

namespace Tab2Sql
{
    /// <summary>
    /// Main Class for the Application.
    /// </summary>
    /// 
    public class Tab2SqlObj
    {

		private static Logger logger = LogManager.GetCurrentClassLogger ();
		
        public System.Text.StringBuilder Diagnostics;
        /// <summary>
        /// The tab value - i.e. backslash plus t.
        /// </summary>
        public const char Tab = '\t';
        /// <summary>
        /// Tab as used in strings.
        /// </summary>
        public const string ProperTab = "    ";

        /// <summary>
        /// # of cols
        /// </summary>
        public int ColCount;

        /// <summary>
        /// comma separated list of names.
        /// </summary>
        public string ColumnNamesList;

        /// <summary>
        /// An array for storing the datatype of each column.
        /// </summary>
        public DataTypes[] ColumnTypes;

        public DataTypes[,] ValueTypes;


        private Err errorLog;

        /// <summary>
        /// highest number in each column
        /// </summary>
        public decimal[] Highest;

        /// <summary>
        /// Table that is created along the way.
        /// </summary>
        public DataTable InMemTable;

        private string inputText;

        /// <summary>
        /// record of longest string in characters.
        /// </summary>
        public int[] LongestLine;

        /// <summary>
        /// lowest number in each column
        /// </summary>
        public decimal[] Lowest;

        /// <summary>
        /// Initiates the tabs2sqloptions object. The way this works is a bit of a mess and will be revised in future versions. 
        /// (Maybe)
        /// </summary>
        public Tab2sqlOptions MyOptions;
        /// <summary>
        /// Default constructor.
        /// </summary>
        public Tab2SqlObj()
        {
            //yo!
            assignDefaults();
        }
        /// <summary>
        /// Constrcutor
        /// </summary>
        /// <param name="input"></param>
        public Tab2SqlObj(string input)
        {
            inputText = input;
            assignDefaults();
        }
        /// <summary>
        /// Diagnostic object.
        /// </summary>
        public Err ErrorLog
        {
            get { return errorLog; }
            set { errorLog = value; }
        }

        /// <summary>
        /// # of rows
        /// </summary>
        public int RowCount { get; set; }

        private void assignDefaults()
        {
            Diagnostics = new System.Text.StringBuilder();
            MyOptions = new Tab2sqlOptions();
            errorLog = new Err("tab2sql");
            ColumnTypes = new DataTypes[300];
            ValueTypes = new DataTypes[300,10000];

            RowCount = 0;
            ColCount = 0;
            ColumnNamesList = "";
            Highest = new decimal[300];
            Lowest = new decimal[300];
            LongestLine = new int[300];
            InMemTable = new DataTable();
        }

        // CONVERTOR METHOD
        // ----------------------------------------------------------------------------
        /// <summary>
        /// Primary Convertor method. Takes the input and then kicks off rest of process.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string Convertor (string input)
        {
        	inputText = input;
        	input = null;
        	try
            {
        		checkIfSpecialParseRulesApply ();
        		MakeTable ();
        		return MakeSQL ();
        	}
            catch (Exception ex)
            {
        		logger.Error (ex.Message);
                //errorLog.Add("Tab2SQL : " + ex.Source + Environment.NewLine + Environment.CurrentDirectory + ex.Message);
                return "Tab2SQL : " + ex.Source + Environment.NewLine + Environment.CurrentDirectory + ex.Message;
            }
        }
        public string Convertor(string input, bool usexTab2Sql)
        {
            Tab2Sql.Classes.Core.xTab2Sql xts = new Tab2Sql.Classes.Core.xTab2Sql(input);
            xts.Process();
            return Convertor(xts.OurDataTable);
        }
       



        
        private void checkIfSpecialParseRulesApply()
        {
            bool isLogFile = Regex.IsMatch(inputText, "#Software:.*#Version:.*#Date:.*#fields:.*",
                                           RegexOptions.Singleline);
            if (isLogFile)
            {
                inputText = SpecialParse.AdjustMSLogs(inputText);
            }
            else
            {
                foo
            }
        }

    }
}